package com.promise.jokerdream.work.signin

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 乐斗游记工作
class PenguinTravelogueWork(uin: String): BaseWork(uin) {

    override val name: String
        get() = "乐斗游记"

    override val priority: Int
        get() = 9

    override val isActWork: Boolean
        get() = true

    private val gameApi = GameApi.Companion.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.Companion.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        return try {
            // 查询乐斗游记信息获取溢出积分
            val queryResult = gameApi.queryPenguinTravelogue()

            queryResult.fold(
                onSuccess = { response ->
                    val overflowScore = response.maxScore

                    // 根据taskInfo中的任务状态决定是否领取奖励
                    response.taskInfo.forEach { task ->
                        if (task.taskStatus == 1) { // 1表示已完成但未领取
                            val rewardResult = gameApi.rewardPenguinTravelogueTask(task.taskId)
                            rewardResult.fold(
                                onSuccess = { rewardResponse ->
                                    historyManager.addHistory(name, rewardResponse.getDescription())
                                },
                                onFailure = { exception ->
                                    historyManager.addHistory(name, exception.message)
                                }
                            )
                        }
                    }

                    // 一键领取
                    val oneClickResult = gameApi.oneClickRewardPenguinTravelogue()
                    oneClickResult.fold(
                        onSuccess = { oneClickResponse ->
                            historyManager.addHistory(name, oneClickResponse.getDescription())
                        },
                        onFailure = { exception ->
                            historyManager.addHistory(name, exception.message)
                        }
                    )

                    // 根据溢出积分兑换传功符，把积分都兑换完
                    var remainingScore = overflowScore
                    var shouldContinue = true
                    while (remainingScore > 0 && shouldContinue) {
                        // 优先兑换10个，如果积分不够则兑换1个
                        val exchangeNum = if (remainingScore >= 10) 10 else 1
                        val exchangeResult = gameApi.exchangePenguinTravelogueItem(exchangeNum)
                        exchangeResult.fold(
                            onSuccess = { exchangeResponse ->
                                historyManager.addHistory(name, exchangeResponse.getDescription())
                                remainingScore -= exchangeNum
                            },
                            onFailure = { exception ->
                                historyManager.addHistory(name, exception.message)
                                // 兑换失败时停止循环，避免无限循环
                                shouldContinue = false
                            }
                        )
                    }

                    WorkResult.Success
                },
                onFailure = { exception ->
                    historyManager.addHistory(name, exception.message)
                    WorkResult.Failure(exception, exception.message)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
}